
Class('addPlan', 'linb.Com',{
    Instance:{
        //base Class for this com
        base:["linb.UI"], 
        //requried class for this com
        required:["linb.UI.Dialog", "linb.UI.Label", "linb.UI.CheckBox", "linb.UI.Input", "linb.UI.ComboInput", "linb.UI.TreeGrid", "linb.UI.Group", "linb.UI.Button", "linb.DataBinder"], 

        properties:{}, 
        parents:null, 
        apself:null, 
        events:{}, 
        iniResource:function(com, threadid){
        }, 
        iniComponents:function(){
            // [[code created by jsLinb UI Builder
            var host=this, children=[], append=function(child){children.push(child.get(0))};
            
            append((new linb.DataBinder)
                .host(host,"databinder1")
                .setName("planinfo")
            );
            
            append((new linb.UI.Dialog)
                .host(host,"dlg_add_plan")
                .setLeft(299)
                .setTop(74)
                .setWidth(676)
                .setHeight(450)
                .setCaption("添加计划")
                .setMinBtn(false)
                .setMaxBtn(false)
                .onRender("_dialog1_onrender")
                .beforeDestroy("_dialog1_beforedestroy")
            );
            
            host.dlg_add_plan.append((new linb.UI.Label)
                .host(host,"label1")
                .setLeft(41)
                .setTop(9)
                .setWidth(70)
                .setCaption("计划名称")
            );
            
            host.dlg_add_plan.append((new linb.UI.Label)
                .host(host,"label2")
                .setLeft(51)
                .setTop(89)
                .setWidth(60)
                .setCaption("指定设备")
            );
            
            host.dlg_add_plan.append((new linb.UI.Label)
                .host(host,"label3")
                .setLeft(-9)
                .setTop(49)
                .setCaption("计划首次开始日期")
            );
            
            host.dlg_add_plan.append((new linb.UI.Label)
                .host(host,"label4")
                .setLeft(360)
                .setTop(50)
                .setWidth(60)
                .setCaption("执行周期")
            );
            
            host.dlg_add_plan.append((new linb.UI.CheckBox)
                .host(host,"status")
                .setDataBinder("planinfo")
                .setDataField("status")
                .setLeft(520)
                .setTop(50)
                .setWidth(90)
                .setCaption("计划有效")
            );
            
            host.dlg_add_plan.append((new linb.UI.Label)
                .host(host,"label5")
                .setLeft(460)
                .setTop(50)
                .setWidth(60)
                .setCaption("计划状态")
            );
            
            host.dlg_add_plan.append((new linb.UI.Input)
                .host(host,"name")
                .setDataBinder("planinfo")
                .setDataField("name")
                .setLeft(121)
                .setTop(9)
                .setWidth(280)
                .onBlur("_name_onblur")
            );
            
            host.dlg_add_plan.append((new linb.UI.Input)
                .host(host,"repeater")
                .setDataBinder("planinfo")
                .setDataField("repeater")
                .setLeft(420)
                .setTop(50)
                .setWidth(40)
                .setValueFormat("^-?(\\d\\d*\\.\\d*$)|(^-?\\d\\d*$)|(^-?\\.\\d\\d*$)")
                .setValue("300")
            );
            
            host.dlg_add_plan.append((new linb.UI.ComboInput)
                .host(host,"deviceid")
                .setDataBinder("planinfo")
                .setDataField("deviceid")
                .setLeft(121)
                .setTop(89)
                .setWidth(509)
                .setItems([{"id":"a", "caption":"item a", "image":"img/demo.gif"}, {"id":"b", "caption":"item b", "image":"img/demo.gif"}, {"id":"c", "caption":"item c", "image":"img/demo.gif"}, {"id":"d", "caption":"item d", "image":"img/demo.gif"}])
                .setValue("a")
                .afterValueSet("_deviceid_aftervalueset")
                .afterUIValueSet("_deviceid_afteruivalueset")
            );
            
            host.dlg_add_plan.append((new linb.UI.TreeGrid)
                .host(host,"monitorgrid")
                .setDock("none")
                .setLeft(121)
                .setTop(129)
                .setWidth(509)
                .setHeight(140)
                .setHeader([{"id":"col1", "width":80, "type":"label", "caption":"col1"}, {"id":"col2", "width":80, "type":"label", "caption":"col2"}, {"id":"col3", "width":80, "type":"label", "caption":"col3"}, {"id":"col4", "width":80, "type":"label", "caption":"col4"}])

            );
            
            host.dlg_add_plan.append((new linb.UI.Label)
                .host(host,"label6")
                .setLeft(41)
                .setTop(129)
                .setWidth(70)
                .setCaption("监控的资源")
            );
            
            host.dlg_add_plan.append((new linb.UI.Group)
                .host(host,"group1")
                .setLeft(80)
                .setTop(280)
                .setWidth(550)
                .setHeight(70)
                .setCaption("报警设定")
            );
            
            host.group1.append((new linb.UI.CheckBox)
                .host(host,"mailalerted")
                .setDataBinder("planinfo")
                .setDataField("mailalerted")
                .setLeft(20)
                .setTop(10)
                .setWidth(90)
                .setCaption("邮件报警")
            );
            
            host.group1.append((new linb.UI.Label)
                .host(host,"label13")
                .setLeft(110)
                .setTop(15)
                .setWidth(90)
                .setCaption("报警邮件地址")
            );
            
            host.group1.append((new linb.UI.Input)
                .host(host,"tomail")
                .setDataBinder("planinfo")
                .setDataField("tomail")
                .setLeft(207)
                .setTop(12)
                .setWidth(310)
            );
            
            host.dlg_add_plan.append((new linb.UI.Button)
                .host(host,"btnSave")
                .setLeft(360)
                .setTop(370)
                .setCaption("保存")
                .onClick("_btnsave_onclick")
            );
            
            host.dlg_add_plan.append((new linb.UI.Button)
                .host(host,"btnCancel")
                .setLeft(500)
                .setTop(370)
                .setCaption("取消")
                .onClick("_btncancel_onclick")
            );
            
            host.dlg_add_plan.append((new linb.UI.Input)
                .host(host,"id")
                .setDataBinder("planinfo")
                .setDataField("id")
                .setLeft(460)
                .setTop(10)
                .setVisibility("hidden")
                .setValue("-1")
            );
            
            host.dlg_add_plan.append((new linb.UI.ComboInput)
                .host(host,"begindate")
                .setDataBinder("planinfo")
                .setDataField("begintime")
                .setLeft(120)
                .setTop(50)
                .setWidth(130)
                .setType("datepicker")
            );
            
            host.dlg_add_plan.append((new linb.UI.Label)
                .host(host,"label22")
                .setLeft(250)
                .setTop(50)
                .setWidth(40)
                .setCaption("时间")
            );
            
            host.dlg_add_plan.append((new linb.UI.ComboInput)
                .host(host,"begintime")
                .setDataBinder("planinfo")
                .setDataField("hms")
                .setLeft(290)
                .setTop(50)
                .setWidth(60)
                .setType("timepicker")
                .setValue("00:00")
            );
            
            return children;
            // ]]code created by jsLinb UI Builder
        }, 
        iniExComs:function(com, hreadid){
        }, 
        setWhoisParent:function(myparents) {
            parents = myparents;
        }, 
        CallRemote:function(cmd,datas,processfun,method){
            var option=null;
            if (method == null || method=="") {
                option = "{'method':'"+"get"+"'}";
            } else {
                option = method;
            }

            linb.request("/NetDM4Web/planmanager.do","cmd="+cmd+"&datas="+datas,
            function(rep, type){
                returnpkg = _.unserialize(rep);
                processfun(returnpkg);
            },
            function() {
                linb.UI.Dialog.alert('提示','网络连接出现问题，无法获得数据，请稍候重试');
            },0,_.unserialize(option));
        },        
        getColIndex:function(sender,col) {
            var hs = sender.getHeader();
            for (var m=0;m<hs.length;m++) {
                if (hs[m].id==col) {
                    return m;
                }
            }
            return -1;
        }, 
        c2v:function(items,c) {
            for (var j=0;j<items.length;j++) {

                if ( c == items[j].caption) {
                    return items[j].id;
                }
            }
        }, 
        v2c:function(items,v) {
            for (var j=0;j<items.length;j++) {
				
                if ( v == items[j].id) {
                	
                    return items[j].caption;
                }
            }
        }, 
        v2cByCol:function(sender,col,items){
            var rows = sender.getRows();
            for (var i=0;i < rows.length;i++) {
                apself.v2cByRowCol(sender,rows[i].id,col,items);
            }
        }, 
        v2cByRowCol:function(sender,x,y,items){
            var v = sender.getCellbyRowCol(x,y).value;
            var colindex = apself.getColIndex(sender,y);
            var c = apself.v2c(items,v);
            var s = "{"+"'"+"value"+"'"+":'"+v+"',"+"'"+"caption"+"'"+":'"+c+"'}";

            var o = _.unserialize(s);
            sender.updateCellByRowCol(x,y,o,false);
        },
        setAllSelector:function(sender,cols) {
            var col = cols.split(',');
            var colindex = -1;
            for (var y=0;y<col.length;y++) {
				colindex = apself.getColIndex(sender,col[y]);
				if (colindex < 0) {
					continue;
				}
				apself.v2cByCol(sender,col[y],sender.getHeader()[colindex].editorListItems);
            }
        },
        getJsonGridDatas:function(cols,commitrow) {
            var currow = commitrow;
            var hs =cols;
            var jsons="";
            var jsondata = "";
            for (var i =0;i < hs.length;i++) {
                if (_.isNumb(currow.cells[i].value)) {
                    jsons = jsons+"'"+hs[i].id+"':"+currow.cells[i].value+"";
                   } else {
	                jsons = jsons+"'"+hs[i].id+"':'"+currow.cells[i].value+"'";
                }
                if (i < hs.length-1) {
                    jsons = jsons+",";
                }
            }
            jsondata = jsondata + "{"+jsons+"}";
            return jsondata;
        },         
        commitds:function(ds) {
            //alert(ds);
            apself.CallRemote('saveres',ds,function(pkg){
        		if (pkg.code == 0) {
	            	apself.$resupdateisok = true;
	        	} else {
	        		apself.$resupdateisok = false;
	        	}
            },"{method:'get',asy:false}");
            
        }, 
        saveres:function(deviceid,sender) {
        	for (var i =0;i<sender.getRows().length;i++) {
        		var rows = apself.getJsonGridDatas(sender.getHeader(),sender.getRows()[i]);
                var jcell = "{'"+"deviceid"+"':"+deviceid+",'"+"rows"+"':"+rows+"}";
				apself.commitds(jcell);
				if (!apself.$resupdateisok) {
					return false;
				}
        	}
        	return true;
        },         
        _btnsave_onclick:function (profile, e, src, value) {
        	if (apself.$foundsameplanname) {
        		linb.UI.Dialog.alert('提示','计划名称命名重复，请重新输入!');
        		
        		return;
        	}
            apself.$hascommited = false;
            var db = apself.databinder1.getValue();
            if (db.status == true) {
            	db.status = 1;
            } else {
            	db.status = 0;
            }
            if (db.mailalerted == true) {
            	db.mailalerted = 1;
            } else {
            	db.mailalerted = 0;
            }
            
            apself.CallRemote('save',_.serialize(db),function(pkg) {
                    if (pkg.code == 0) {
                        apself.$hascommited = true;
                    } else {
                        apself.$hascommited = false;
                        linb.UI.Dialog.alert('提示',pkg.messages);
                    }
                },"{'method':'get',asy:false}");
            if (apself.$hascommited == true) {
                //循环提交资源表格中的数据
                if (apself.saveres(apself.deviceid.getValue(),apself.monitorgrid)) {
                	linb.UI.Dialog.alert('提示','数据保存完成!');
                } else {
                	linb.UI.Dialog.alert('提示','数据保存失败，请与管理员联系!');
                };
            }
        }, 
        _btncancel_onclick:function (profile, e, src, value) {
            apself.dlg_add_plan.close();
        }, 
        _deviceid_aftervalueset:function (profile, oldValue, newValue) {
            if (apself.deviceid.tag == 0) {
                apself.CallRemote('lr','{deviceid:'+newValue+'}',function(pkg) {
                    apself.$row = null;
                    if (pkg.code==0) {
                        var so = _.unserialize(pkg.datas);
                        var headerinfo = _.unserialize(so.header);
                        apself.monitorgrid.setHeader(headerinfo.struct);
                        var hs = headerinfo.hidelist.split(',');
                        for (var x=0;x < hs.length;x++) {
                            apself.monitorgrid.showColumn(hs[x],false);
                        }
                        apself.monitorgrid.setRows(_.unserialize(so.rows));                        
                        apself.setAllSelector(apself.monitorgrid,headerinfo.selectorlist);
                    } else {
                        linb.UI.Dialog.alert('提示','网络连接出现问题，无法获得数据，请稍候重试');
                    }
                },
                "{'method':'get',asy:false}");
             }
        }, 
        _dialog1_onrender:function (profile) {
            apself= this;
            apself.deviceid.tag = -1;
            apself.monitorgrid.removeAllRows();
            apself.begindate.setValue(new Date());
            apself.CallRemote('ld','',function(pkg){
                if (pkg.code== 0) {
                    apself.deviceid.setItems(_.unserialize(pkg.datas));
                    if (apself.deviceid.getItems().length > 0) {
                        apself.deviceid.tag = 0;
                        apself.deviceid.setValue(apself.deviceid.getItems()[0].id);
                    }
                } else {
                    linb.UI.Dialog.alert('提示','网络连接出现问题，无法获得数据，请稍候重试');
                }
            },"{'method':'get',asy:true}");
            apself.$row = null;
            apself.$foundsameplanname = false;

        }, 
        _name_onblur:function (profile) {
            if (apself.name.getUIValue() != '') {
            apself.CallRemote('same',"{pname:'"+apself.name.getUIValue()+"'}",function(pkg) {
                if (pkg.code == 0) {
                    apself.$foundsameplanname = true;
                } else {
                     apself.$foundsameplanname = false;
                }
            });
           }
        }, 
        _dialog1_beforedestroy:function (profile) {
            if (parents != null) {
                oo = _.unserialize(parents.menubar1.getTagVar());
                oo.madd_plan = 0;
                parents.menubar1.setTagVar(_.serialize(oo));
            }
            linb.DataBinder.destroyAll();
            return true;
        }, 
        _deviceid_afteruivalueset:function (profile, oldValue, newValue) {
            if (apself.deviceid.tag == 0) {
                apself.deviceid.setValue(newValue);
            }
        }
    }
});